﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.Text;
using System.Xml;
using NLog;

namespace LogService
{
    class SilverlightPolicyProvider : ISilverlightPolicyProvider
    {
        #region ISilverlightPolicyProvider Members
        public Message ProvidePolicyFile()
        {
            string xml =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers=""*"">
                <domain uri=""*""/>
            </allow-from>
            <grant-to>
                <resource path=""/"" include-subpaths=""true""/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>";

            XmlReader reader = XmlReader.Create(StringToStream(xml));
            Message result = Message.CreateMessage(MessageVersion.None, "", reader);
            return result;
        }
        Stream StringToStream(string result)
        {
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }
        #endregion

    }

    [ServiceContract]
    interface ISilverlightPolicyProvider
    {
        [OperationContract]
        [WebGet(UriTemplate = "ClientAccessPolicy.xml")]
        Message ProvidePolicyFile();
    }

}
